package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.activity.GSPaymentActivity;
import com.jt.tenant.activity.OilQrcodeActivity;
import com.jt.tenant.activity.PromptActivity;
import com.jt.tenant.adapter.GasStationListAdapter;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetGSOrderId;
import com.jt.tenant.conn.GetGasStationList;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.activity.AppV4Fragment;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

public class RefuelActivity extends JTActivity implements View.OnClickListener {

    private TextView title;

    private ImageView iv_srcoll_oil;

//    private LinearLayout lin_oil_today_info;

//    private TextView tv_oil_num,tv_oil_cost,tv_oil_meilage;

    private ListView mGasStationList;

    private List<GetGasStationList.GSList> mList = new ArrayList<>();

    private GasStationListAdapter mGasStationListAdapter;

    private GetGasStationList mGetGasStationList;

    private GetGSOrderId getGSOrderId;

    private String code;

//    private ImageView promptBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_refuel_oil);

        initView();

    }

    @Override
    protected void onResume() {

        super.onResume();

        mGetGasStationList.execute(this);

    }

    private void initView() {

        (title = (TextView)findViewById(R.id.tv_title_name)).setText("加油");

        iv_srcoll_oil = (ImageView)findViewById(R.id.iv_srcoll_oil);

//        promptBtn = (ImageView)findViewById(R.id.prompt_btn);

//        promptBtn.setOnClickListener(this);

        mGasStationList = (ListView)findViewById(R.id.gas_station_list);

        mGasStationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                switch (mList.get(position).FLAG){
                    case "0":
//                        startActivity(new Intent(getActivity(),SelectOilActivity.class).putExtra("id",mList.get(position).ID));

                        getGSOrderId = new GetGSOrderId(mList.get(position).ID,new AsyCallBack<GetGSOrderId.Info>(){

                            @Override
                            public void onSuccess(String toast, int type, GetGSOrderId.Info info) throws Exception {
                                super.onSuccess(toast, type, info);

                                if(info.msg.equals("000")){

                                    startActivity(new Intent(RefuelActivity.this,OilQrcodeActivity.class)

                                            .putExtra("gs_name",mList.get(position).GAS_NAME)

                                            .putExtra("gs_phone",mList.get(position).GAS_PHONE)

                                            .putExtra("gs_logo",mList.get(position).URL)

                                            .putExtra("gs_address",mList.get(position).GAS_ADDRESS)

                                            .putExtra("lon",mList.get(position).GAS_LON)

                                            .putExtra("order_id",info.GasCode)

                                            .putExtra("lat",mList.get(position).GAS_LAT)
                                    );

                                }

                            }
                        });

                        getGSOrderId.execute(RefuelActivity.this);

                        break;
                    case "1":
                        startActivity(new Intent(RefuelActivity.this,GSPaymentActivity.class)
                                .putExtra("id",mList.get(position).ID)
                                .putExtra("gs_phone",mList.get(position).GAS_PHONE)
                                .putExtra("gs_name",mList.get(position).GAS_NAME)
                                .putExtra("gs_address",mList.get(position).GAS_ADDRESS)
                                .putExtra("order_id",mList.get(position).GASLOG.gasCode)
                                .putExtra("total_price",mList.get(position).GASLOG.gasTotalMoney)
                                .putExtra("lon",mList.get(position).GAS_LON)
                                .putExtra("lat",mList.get(position).GAS_LAT)
                                .putExtra("gs_logo",mList.get(position).URL)


                        );

                        break;
                    case "2":
                        startActivity(new Intent(RefuelActivity.this,OilQrcodeActivity.class)
                                .putExtra("gs_name",mList.get(position).GAS_NAME)
                                .putExtra("gs_phone",mList.get(position).GAS_PHONE)
                                .putExtra("gs_address",mList.get(position).GAS_ADDRESS)
                                .putExtra("order_id",mList.get(position).GASLOG.gasCode)
                                .putExtra("lon",mList.get(position).GAS_LON)
                                .putExtra("lat",mList.get(position).GAS_LAT)
                                .putExtra("gs_logo",mList.get(position).URL)

                        );

                        break;
                    default:
                        break;
                }
            }
        });
        mGasStationListAdapter = new GasStationListAdapter(this,mList);

        mGasStationList.setAdapter(mGasStationListAdapter);

        mGetGasStationList = new GetGasStationList(new AsyCallBack<GetGasStationList.Info>(){
            @Override
            public void onSuccess(String toast, int type, GetGasStationList.Info info) throws Exception {
                super.onSuccess(toast, type,info);
                mList.clear();

                code = info.code;

                mList.addAll(info.gsLists);

                JTApplication.UtilAsyncBitmap.get(info.logourl,iv_srcoll_oil, R.mipmap.loading);

                mGasStationListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if(!toast.equals("")){

                    UtilToast.show(RefuelActivity.this, toast);

                }
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

//            case R.id.prompt_btn:
//
//                Intent intent = new Intent(this, PromptActivity.class);
//                intent.putExtra("flag", "oil");
//                startActivity(intent);
//
//                break;

        }

    }

//    private SpannableString oil_sum(String str,String unit){
//
//        SpannableString s = new SpannableString("约"+str+unit);
//
//        s.setSpan(new RelativeSizeSpan(1.0f), 0, 1, 0);
//
//        s.setSpan(new RelativeSizeSpan(1.6f),1, str.length()+1, 0);
//
//        s.setSpan(new RelativeSizeSpan(1.3f), s.length()-unit.length(), s.length(), 0);
//
//        return s;
//
//    }
}
